package cn.ml.web1662.web.servlet;


import cn.ml.web1662.commons.DateUtils;
import cn.ml.web1662.entity.Pagination;
import cn.ml.web1662.entity.Student;
import cn.ml.web1662.service.StudentService;
import cn.ml.web1662.service.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class StudentServlet extends HttpServlet {
    // doGet doPost
    private StudentService service = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        System.out.println(uri);
        String option = uri.substring(uri.lastIndexOf("/")+1);
        System.out.println(option);
        if("add".equals(option)){
            add(req,resp);
        }
        if("update".equals(option)){
            update(req,resp);
        }
        if("show".equals(option)){
            show(req,resp);
        }
        if("list".equals(option)){
            list(req,resp);
        }
        if("delete".equals(option)){
            delete(req,resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        service.delete(id);
        resp.sendRedirect("list");
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //request.setAttribute("students",service.list());
        //分页
        String pageIndexStr = request.getParameter("currentPageIndex");
        int currentPageIndex = 1;
        if(pageIndexStr!=null && pageIndexStr.matches("\\d+")){ //为数字
            currentPageIndex = Integer.parseInt(pageIndexStr);
        }
        int pageSize = 3;
        Pagination<Student> pagination = new Pagination<>();
        pagination.setCurrentPageIndex(currentPageIndex);
        pagination.setPageSize(pageSize);
        service.listByPagination(pagination);
        request.setAttribute("pagination",pagination);

        request.getRequestDispatcher("/WEB-INF/student/list.jsp").forward(request,response);
       /*Gson gson = new Gson();
       String json = gson.toJson(service.list());
       response.setContentType("application/json;charset=utf-8");
       PrintWriter out = response.getWriter();
       out.print(json);
       out.flush();
       out.close();*/
    }

    private void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        request.setAttribute("student",service.get(id));
        request.getRequestDispatcher("/WEB-INF/student/show.jsp").forward(request,response);
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idStr = request.getParameter("id");
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        String bornday = request.getParameter("bornday");
        Student student =null;
        Map map = new HashMap();
        try {
            if (idStr != null && idStr.matches("\\d+")) {
                //修改的与误操作
                student = new Student(Integer.parseInt(idStr), name, gender, DateUtils.strConvertDate(bornday, "yyyy-MM-dd'T'HH:mm"));
                service.update(student);
                //map.put("msg", "更新成功");
                //map.put("flag", true);
            } else {
                //新增的业务操作
                student = new Student(name, gender, DateUtils.strConvertDate(bornday, "yyyy-MM-dd'T'HH:mm"));
                service.save(student);
                //map.put("msg", "添加成功");
                //map.put("flag", true);
            }
        }catch (Exception e){
            map.put("msg","操作失败");
            map.put("flag",false);
        }

        response.sendRedirect("show?id="+student.getId());
        //{"msg":"xxxx","flag":xx}
        /*Gson gson = new Gson();
        String json = gson.toJson(map);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(json);
        out.flush();
        out.close();*/

    }

    private void add(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String idStr = request.getParameter("id");
        if(idStr!=null&&idStr.matches("\\d+")){
            //修改
            request.setAttribute("title","修改");
            //数据库中现根据id提取student
            //Student student = new Student(Integer.parseInt(idStr),"张三","男",new Date());
            request.setAttribute("student",service.get(idStr));
        }else{
            //添加
            request.setAttribute("title","新增");
        }
        request.getRequestDispatcher("/WEB-INF/student/edit.jsp").forward(request,response);
    }
}
